package com.strange3.designpatterns.behavioral.command
{
	
	
	[Bindable]
	public class CommandsExplorer
	{
		private var _commitedCommands:Array;
		private var _currentCommandIndex:int;
		private static var _instance:CommandsExplorer = null;
		
		public static function get instance():CommandsExplorer {
			if (!_instance) {
				_instance = new CommandsExplorer()
			}
			return _instance;
		}
		public function get hasCommadsToRedo():Boolean {
			if (_currentCommandIndex < _commitedCommands.length - 1) {
				return true;
			}
			return false;
		}

		public function get hasCommandsToUndo():Boolean {
			if (_currentCommandIndex >= 0) {
				return true;
			}
			return false;
		}
		
		public function doCommand(c:Command):void {
			c.execute();
			_currentCommandIndex++;
			_commitedCommands[_currentCommandIndex] = c;
			
		}
		
		public function undoCommand():void {
			if (hasCommandsToUndo) {					
				var commitedCommand:Command = _commitedCommands[_currentCommandIndex];
				_currentCommandIndex--;
				commitedCommand.unExecute();
				
			}
		}
		public function redoCommand():void {
			if(hasCommadsToRedo) {
				_currentCommandIndex++;
				var c:Command = _commitedCommands[_currentCommandIndex];
				c.execute();
				
			}
		}
		
		public function CommandsExplorer()
		{
			_currentCommandIndex = -1;
			_commitedCommands = new Array();
		}

	}
}